<?php
class Content extends MY_Controller{
	function __construct() {
		parent::__construct();
	}
	
	function css($filename, $p1='', $p2='', $p3=''){
		//$this->protect_referer();
		
		$template = $this->web_option('CURRENT_THEME');
		
		$filename	= $filename . ($p1!='' ? '/'.$p1 : '') . ($p2!='' ? '/'.$p2 : '') . ($p3!='' ? '/'.$p3 : '');
		if(!file_exists(APPPATH . 'views/templates/' . $template . '/css/' . $filename)) $template = 'standard';

		if(extension_loaded('zlib')){
			//PHP gzip method
			//ob_start ("ob_gzhandler");
			
			//Header to tell browser what kind of file it is.
			header("Content-type: text/css; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}
		
		$content	= file_get_contents(APPPATH . 'views/templates/' . $template . '/css/' . $filename);
		$content	= str_replace('_BASE_URL', base_url(),$content);
		//$content	= compressResponse(str_replace(' url', '##url',$content));
		//$content	= str_replace('##url', ' url',$content);
		echo $content;
	}
	
	function js($filename, $p1='', $p2='', $p3='', $p4='', $p5='', $p6='', $p7='', $p8='', $p9='', $p10='', $p11='', $p12='', $p13='', $p14='', $p15=''){
		//$this->protect_referer();
		
		$filename	= $filename . ($p1!='' ? '/'.$p1 : '') . ($p2!='' ? '/'.$p2 : '') . ($p3!='' ? '/'.$p3 : '') . ($p4!='' ? '/'.$p4 : '') . ($p5!='' ? '/'.$p5 : '') . ($p6!='' ? '/'.$p6 : '') . ($p7!='' ? '/'.$p7 : '') . ($p8!='' ? '/'.$p8 : '') . ($p9!='' ? '/'.$p9 : '') . ($p10!='' ? '/'.$p10 : '') . ($p11!='' ? '/'.$p11 : '') . ($p12!='' ? '/'.$p12 : '') . ($p13!='' ? '/'.$p13 : '') . ($p14!='' ? '/'.$p14 : '') . ($p15!='' ? '/'.$p15 : '');
		
		$template = $this->web_option('CURRENT_THEME');
		if(!file_exists(APPPATH . 'views/templates/' . $template . '/js/' . $filename)) $template = 'standard';
		
		$ext	= @end(explode('.', $filename));
		
		if($ext == 'js' && extension_loaded('zlib')){
			//ob_start ("ob_gzhandler");
			header("Content-type: text/javascript; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}else if($ext == 'css' && extension_loaded('zlib')){
			//ob_start ("ob_gzhandler");
			header("Content-type: text/css; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}else{
			//ob_start ("ob_gzhandler");
			header("Content-type: text/html; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}
		
		if(substr(strtolower($filename),0,6)=='jquery' || substr(strtolower($filename),0,8)=='calendar'){
			$content	= file_get_contents(APPPATH . 'views/templates/' . $template . '/js/' . $filename);
			$content	= str_replace('_BASE_URL', base_url(),$content);
			echo $content;
		}else{
			$content	= file_get_contents(APPPATH . 'views/templates/' . $template . '/js/' . $filename);
			$content	= str_replace('_BASE_URL', base_url(),$content);
			//$content	= compressResponse($content);
			$content	= str_replace('elseif', 'else if',$content);
			echo $content;
			//echo file_get_contents(APPPATH . 'views/templates/' . $template . '/js/' . $filename);
		}
	}
	
	function images($filename){
		//$this->protect_referer();
		
		$template = $this->web_option('CURRENT_THEME');
		$path = APPPATH . 'views/templates/' . $template . '/images/' . $filename;
		
		if(file_exists($path)){
			$size = getimagesize($path);
			header("Content-type: {$size['mime']}");
			echo file_get_contents($path);
		}else if(file_exists(APPPATH . 'views/templates/standard/images/' . $filename)){
			$path = APPPATH . 'views/templates/standard/images/' . $filename;
			$size = getimagesize($path);
			header("Content-type: {$size['mime']}");
			echo file_get_contents($path);
		}
	}
	
	function media($param1, $param2=''){
		//$this->protect_referer();
		
		$filename	= ($param2 == '' ? $param1 : $param2);
		
		$post_id	= $this->get_data("SELECT post_id FROM bl_content WHERE site_id = '".CURRENT_SITE."' AND post_type = 'image' AND LOWER(post_title) = '".@ereg_replace("[^a-zA-Z0-9._-]", "",@strtolower(@trim($filename)))."'");
		$filename	= 'post_'.$post_id . '.' . end(explode('.',$filename));
		
		if($param2==''){
			$path = APPPATH . 'file/media/' . $filename;
		}else{
			$path = APPPATH . 'file/cache/' . $filename;
		}
		
		if(file_exists($path)){
			$size = getimagesize($path);
			header("Content-type: {$size['mime']}");
			echo file_get_contents($path);
		}
	}
	
	function vendor($filename, $p1='', $p2='', $p3='', $p4='', $p5='', $p6='', $p7='', $p8='', $p9='', $p10='', $p11='', $p12='', $p13='', $p14='', $p15=''){
		//$this->protect_referer();
		
		$filename	= $filename . ($p1!='' ? '/'.$p1 : '') . ($p2!='' ? '/'.$p2 : '') . ($p3!='' ? '/'.$p3 : '') . ($p4!='' ? '/'.$p4 : '') . ($p5!='' ? '/'.$p5 : '') . ($p6!='' ? '/'.$p6 : '') . ($p7!='' ? '/'.$p7 : '') . ($p8!='' ? '/'.$p8 : '') . ($p9!='' ? '/'.$p9 : '') . ($p10!='' ? '/'.$p10 : '') . ($p11!='' ? '/'.$p11 : '') . ($p12!='' ? '/'.$p12 : '') . ($p13!='' ? '/'.$p13 : '') . ($p14!='' ? '/'.$p14 : '') . ($p15!='' ? '/'.$p15 : '');
		$ext		= strtolower(@end(explode('.', $filename)));
		
		if($ext == 'js' && extension_loaded('zlib')){
			//ob_start ("ob_gzhandler");
			header("Content-type: text/javascript; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}else if($ext == 'css' && extension_loaded('zlib')){
			//ob_start ("ob_gzhandler");
			header("Content-type: text/css; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}else if(in_array($ext,array('jpg','jpeg')) && extension_loaded('zlib')){
			//ob_start ("ob_gzhandler");
			header("Content-type: {$size['mime']}");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}else if(in_array($ext,array('php'))) {
			
		}else{
			//ob_start ("ob_gzhandler");
			header("Content-type: text/html; charset: UTF-8");
			
			//Caching
			header("Cache-Control: must-revalidate");
			$expires = "Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT";
			header($expires);
		}
		
		if(in_array($ext,array('php'))){
			include_once(APPPATH . 'third_party/' . $filename);
		}else{
			$content	= file_get_contents(APPPATH . 'third_party/' . $filename);
			$content	= str_replace('_BASE_URL', base_url(),$content);
			echo $content;
		}
	}
}
?>